<?php

/**
 * content_types actions.
 *
 * @package    master_directory
 * @subpackage content_types
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class content_typesActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->form = new ContentTypeForm();
  	
    $this->content_types = Doctrine_Core::getTable('ContentType')->retreiveActiveContentTypes();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->content_type = Doctrine_Core::getTable('ContentType')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->content_type);
    
    $this->form = new ContenttypeForm($this->content_type);
    
    return $this->renderPartial('content_type',array('form'=>$this->form)); 
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ContentTypeForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $form = new ContentTypeForm();

    $this->processForm($request, $form);
    
    $this->content_types = Doctrine_Core::getTable('ContentType')->retreiveActiveContentTypes();
    
    $this->form = new ContentTypeForm();
    
    return $this->renderPartial('content_type_container',array('content_types' => $this->content_types, 'form'=>$this->form));

    //$this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($content_type = Doctrine_Core::getTable('ContentType')->find(array($request->getParameter('id'))), sprintf('Object content_type does not exist (%s).', $request->getParameter('id')));
    $this->form = new ContentTypeForm($content_type);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($content_type = Doctrine_Core::getTable('ContentType')->find(array($request->getParameter('id'))), sprintf('Object content_type does not exist (%s).', $request->getParameter('id')));
    $form = new ContentTypeForm($content_type);

    $this->processForm($request, $form);

    //$this->setTemplate('edit');
    
    $this->content_types = Doctrine_Core::getTable('ContentType')->retreiveActiveContentTypes();
    
    $this->form = new ContentTypeForm();
    
    return $this->renderPartial('content_type_container',array('content_types' => $this->content_types, 'form'=>$this->form));
  }

  public function executeDelete(sfWebRequest $request)
  {
   // $request->checkCSRFProtection();

    $this->forward404Unless($content_type = Doctrine_Core::getTable('ContentType')->find(array($request->getParameter('id'))), sprintf('Object content_type does not exist (%s).', $request->getParameter('id')));
    $content_type->setDeleted(1);
    $content_type->save();
    
    $this->content_types = Doctrine_Core::getTable('ContentType')->retreiveActiveContentTypes();
    
    $this->form = new ContentTypeForm();
    
    return $this->renderPartial('content_type_container',array('content_types' => $this->content_types, 'form'=>$this->form));

    //$this->redirect('content_types/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $content_type = $form->save();

      //$this->redirect('content_types/edit?id='.$content_type->getId());
    }
  }

  
}
